

import org.zjx.db.ItemInstanceDB;
import org.zjx.db.ItemSlotDB;
import org.zjx.db.ItemTemplateDB;
import org.zjx.enums.prop.SlotType;
import org.zjx.model.prop.ItemSlot;
import org.zjx.model.prop.ItemTemplate;
import org.zjx.repository.ItemInstanceRepository;
import org.zjx.repository.ItemSlotRepository;
import org.zjx.repository.ItemTemplateRepository;
import org.zjx.repository.impl.ItemInstanceRepositoryImpl;
import org.zjx.repository.impl.ItemSlotRepositoryImpl;
import org.zjx.repository.impl.ItemTemplateRepositoryImpl;
import org.zjx.service.ItemManager;
import org.zjx.service.ItemStackService;
import org.zjx.service.ItemValidator;
import org.zjx.service.impl.ItemManagerImpl;

import java.util.List;

public class ItemInstanceData {
    public static void main(String[] args) {
        // 创建表
        ItemTemplateDB templateDB = new ItemTemplateDB();
        ItemSlotDB slotDB = new ItemSlotDB();
        ItemInstanceDB instanceDB = new ItemInstanceDB();

        ItemInstanceRepository instanceRepo = new ItemInstanceRepositoryImpl(instanceDB);
        ItemSlotRepository slotRepo = new ItemSlotRepositoryImpl(slotDB);
        ItemTemplateRepository templateRepo = new ItemTemplateRepositoryImpl(templateDB);

        ItemStackService stackService = new ItemStackService(templateRepo);
        ItemValidator validator = new ItemValidator(slotRepo, templateRepo);

        ItemManager itemManager = new ItemManagerImpl(instanceRepo, slotRepo, templateRepo, stackService, validator);


        List<ItemSlot> itemSlots = new ItemSlotDB().findByType(SlotType.INVENTORY);
        create(itemManager, "123456", "黄色合成旗", itemSlots.get(1), 1);
        create(itemManager, "123456", "四叶花", itemSlots.get(2), 2);
        create(itemManager, "123456", "夜光珠", itemSlots.get(3), 1);
        create(itemManager, "123456", "朱雀石", itemSlots.get(6), 6);
        create(itemManager, "123456", "百味酒", itemSlots.get(7), 1);
        create(itemManager, "123456", "十香返生丸", itemSlots.get(8), 1);
        create(itemManager, "123456", "飞行符", itemSlots.get(11), 30);
        create(itemManager, "123456", "摄妖香", itemSlots.get(12), 10);
        create(itemManager, "123456", "高级宠物口粮", itemSlots.get(13), 13);


        List<ItemSlot> extendedSlots = new ItemSlotDB().findByType(SlotType.EXTENDED_INVENTORY);
        create(itemManager, "123456", "布衣", extendedSlots.get(3), 1);
        create(itemManager, "123456", "缎带", extendedSlots.get(5), 2);
        create(itemManager, "123456", "绿靴", extendedSlots.get(6), 1);
        create(itemManager, "123456", "七彩石囊", extendedSlots.get(7), 1);
        create(itemManager, "123456", "天眼通符", extendedSlots.get(12), 13);
        create(itemManager, "123456", "钹", extendedSlots.get(13), 1);
        create(itemManager, "123456", "分解符", extendedSlots.get(16), 30);
        create(itemManager, "123456", "佛光舍利子", extendedSlots.get(18), 1);
        create(itemManager, "123456", "五龙丹", extendedSlots.get(19), 1);

    }

    public static void create(ItemManager itemManager, String playerId, String itemName, ItemSlot itemSlot, int count) {
        ItemTemplate itemTemplate = new ItemTemplateDB().findByName(itemName);
        if (itemTemplate == null) throw new RuntimeException("道具模板不存在：" + itemName);
        if (count > itemTemplate.getMaxStack() || count < 0)
            throw new RuntimeException(itemName + "-数量超出范围：" + count);
        itemManager.createItem(playerId, itemTemplate.getId(), itemSlot.getId(), count);
    }
}
